import java.io.Serializable;
import java.util.ArrayList;

/*
 * ROUND STARTS:
 * SET PLAYER ORDER
 * SET PLAYER TIMES
 * CALCULATE PRODUCTION
 * IF ROUND 1 OR 2: SELECTION PHASE
 * EACH PLAYER TAKES TURN
 * ROUND ENDS
 */
/**
 * The Round class that controls a round for MULE and takes turns and calculates production
 * @author srinidhi
 *
 */
public class Round implements Serializable{
	//used for serialization
	private static final long serialVersionUID = 1L;
	//the number of the round
	private int roundNum;
	//the turn that is currently active within the round
	private Turn turn;
	//the GameModel instance
	private GameModel gm = GameModel.getInstance();
	//creates a random event for all players
	
	/**
	 * no args constructor for Round that makes it take turns and calculate production
	 */
	public Round() 
	{
		calculateProduction();
		new RandomEvent(gm.getPlayers());
	}
	
	
	/**
	 * returns the current turn in the round
	 * @return the current turn
	 */

	public Turn getTurn() {
		return turn;
	}
	
	/**
	 * Gets the number of the round
	 * @return the number of the round
	 */

	public int getRoundNum() {
		return roundNum;
	}
	
	
	public void save() {
		//save
		GameModel.getInstance().save("game.dat");
	}
	
	/**
	 * Sets the round's number.
	 * @param i the round number
	 */

	public void setRoundNum(int i) {
		roundNum = i;
	}
	/**
	 * Takes each player's resources and adds resources for round
	 */
	public void calculateProduction(){
		if ((GameModel.getInstance().getRoundsLeft() != 10 && GameModel.getInstance().getDifficulty().equalsIgnoreCase("EASY"))||
				(GameModel.getInstance().getRoundsLeft() != 12 && GameModel.getInstance().getDifficulty().equalsIgnoreCase("Hard"))) {
			System.out.println("CALCULATING PRODUCTION" + "\n");
			for (Player player : gm.getPlayers()){
				for (Mule mule : player.getMules()){
					mule.produce();
				}
				
				if (player.getPlayerProperties() != null){
					for (Property property : player.getPlayerProperties()){
						if (property != null && property.hasMule() && property.getMule().hasEnergy()){
	
							if (property.getTerrainType().equalsIgnoreCase("River")){
								//4 food, 2 energy
								player.setPlayerResource(0, player.getPlayerResource(0) + 4);
								player.setPlayerResource(1, player.getPlayerResource(1) + 2);
							}
	
							else if (property.getTerrainType().equalsIgnoreCase("Plain")){
								//2 food, 3 energy, 1 smithOre
								player.setPlayerResource(0, player.getPlayerResource(0) + 2);
								player.setPlayerResource(1, player.getPlayerResource(1) + 3);
								player.setPlayerResource(2, player.getPlayerResource(2) + 1);
							}
	
							else if (property.getTerrainType().equalsIgnoreCase("Mountain One")){
								//1 food, 1 energy, 2 smithOre
								player.setPlayerResource(0, player.getPlayerResource(0) + 1);
								player.setPlayerResource(1, player.getPlayerResource(1) + 1);
								player.setPlayerResource(2, player.getPlayerResource(2) + 2);
							}
	
							else if (property.getTerrainType().equalsIgnoreCase("Mountain Two")){
								//1 food, 1 energy, 3 smithOre
								player.setPlayerResource(0, player.getPlayerResource(0) + 1);
								player.setPlayerResource(1, player.getPlayerResource(1) + 1);
								player.setPlayerResource(2, player.getPlayerResource(2) + 3);
							}
	
							else if (property.getTerrainType().equalsIgnoreCase("Mountain Three")){
								//1 food, 1 energy, 4 smithOre
								player.setPlayerResource(0, player.getPlayerResource(0) + 1);
								player.setPlayerResource(1, player.getPlayerResource(1) + 1);
								player.setPlayerResource(2, player.getPlayerResource(2) + 4);
							}
						}
					}
				}
			}
		}
	}
	/**
	 * The turn loop that works based off the number of players
	 */
	public void takeTurns() {
		for(int j = 0; j < GameModel.getInstance().getPlayers().size(); j++){
			Player p = GameModel.getInstance().getPlayers().get(j);
			//creates a turn of default time 50s with the Player p
			//setting the table limits for turns
			if(roundNum < 4) p.setTableLim(3);
			else if (roundNum < 8) p.setTableLim(4);
			else p.setTableLim(5);
			//if the Player p has a partial shortage, changes their turn time to 30s
			if(p.isPartialShortage()) p.setTurnTime(30);
			//if the Player p has a total shortage, changes their turn time to 5s
			else if(p.isTotalShortage()) p.setTurnTime(5);
			//otherwise, the player's time should be 50s
			else p.setTurnTime(50);
			Turn turn = new Turn(p);
		}
		save();
	}
	
}
